﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;


namespace _1stVATCWin
{
    public partial class AddLoai : Form
    {
        public string id;
        public AddLoai(string id)
        {
            
            InitializeComponent();
            this.id = id;
            loadcombobox();
        }
       
        private void loadcombobox()
        {

            Config file = new Config(Application.StartupPath + "\\config.ini");
            string connectionString = file.ReadValue("server", "conn");
            SqlConnection conn = new SqlConnection(connectionString);
            string sql = "select * from LOAISANPHAM";
            SqlCommand cmd = new SqlCommand(sql, conn);

            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            conn.Open();
            DataSet ds = new DataSet();
            adapter.Fill(ds, "Table LOAISANPHAM");
            DataTable dt = ds.Tables["Table LOAISANPHAM"];

            comboBox1.DisplayMember = "IDloai";
            comboBox1.ValueMember = "IDloai";
            comboBox1.DataSource = ds.Tables["Table LOAISANPHAM"].DefaultView;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //if ((MessageBox.Show("Bạn có muốn thêm dữ liệu không?", "Thông Báo", MessageBoxButtons.OKCancel)) == DialogResult.OK)
            Config file = new Config(Application.StartupPath + "\\config.ini");
            string connectionString = file.ReadValue("server", "conn");
            //string connectionString = @"Data Source=FOX-PC\SQLEXPRESS;Initial Catalog=Project;Integrated Security=True";
            SqlConnection conn = new SqlConnection(connectionString);
            string sql = "select IDloai from NCC_CT where IDloai=@IDloai and IDNCC=@IDNCC";
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.Parameters.AddWithValue("@IDloai", comboBox1.SelectedValue);
            cmd.Parameters.AddWithValue("@IDNCC", id);

            conn.Open();
            SqlDataReader r = cmd.ExecuteReader();
            if (r.Read())
            {
                label3.Text = "Sản phẩm đã có trong hợp đồng";
                r.Close();
            }
            else
            {
                r.Close();
                //this.DialogResult = DialogResult.OK;
                //Config file = new Config(Application.StartupPath + "\\config.ini");
                //string connectionString = file.ReadValue("server", "conn");
                //SqlConnection conn = new SqlConnection(connectionString);
                //string sql = "insert into NCC_CT(IDNCC,IDloai) values (@idncc,@idloai)";
                //SqlCommand cmd = new SqlCommand(sql, conn);
                r.Close();
               
                this.DialogResult = DialogResult.OK;

                cmd.Parameters.Clear();
                cmd.CommandText = "insert into NCC_CT(IDNCC,IDloai) values (@idncc,@idloai)";               
                cmd.Parameters.AddWithValue("@idncc", id);
                cmd.Parameters.AddWithValue("@idloai", comboBox1.SelectedValue);

                
                cmd.ExecuteNonQuery();
                conn.Close();
            }
        }


       
    }
}
